package com.cc.visitor.service;

import com.cc.visitor.dao.LogLoginMapper;
import com.cc.visitor.dao.UserWeixinMapper;
import com.cc.visitor.entity.LogLogin;
import com.cc.visitor.entity.UserWeixin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author chanchaw
 * @create 2024-09-22 17:47
 */
@Service
public class LogLoginServiceImpl implements LogLoginService{
    @Autowired
    private LogLoginMapper dao;
    @Autowired
    private UserWeixinMapper userWeixinMapper;
    @Override
    public LogLogin insert(LogLogin record) {
        int insert = dao.insert(record);
        Integer id = Optional.ofNullable(record.getId()).orElse(0);
        return dao.selectOneById(id);
    }

    @Override
    public LogLogin insert8OpenId(String openId){
        openId = Optional.ofNullable(openId).orElse("");
        UserWeixin userWeixin = userWeixinMapper.get8OpenId(openId);
        if(userWeixin == null) return null;
        LogLogin logLogin = new LogLogin();
        logLogin.setOpenId(userWeixin.getOpenId());
        logLogin.setUnionId(userWeixin.getUnionId());
        logLogin.setNickName(userWeixin.getNickName());
        logLogin.setRealName(userWeixin.getRealName());
        int insert = dao.insert(logLogin);
        Integer id = Optional.ofNullable(logLogin.getId()).orElse(0);
        return dao.selectOneById(id);
    }
}
